package com.cmict.data.bpm.api.task;

import com.cmict.data.bpm.api.task.dto.*;
import com.cmict.data.bpm.controller.admin.task.vo.task.BpmTaskDonePageItemRespVO;
import com.cmict.data.bpm.controller.admin.task.vo.task.BpmTaskDonePageReqVO;
import com.cmict.data.bpm.controller.admin.task.vo.task.BpmTaskTodoPageItemRespVO;
import com.cmict.data.bpm.controller.admin.task.vo.task.BpmTaskTodoPageReqVO;
import com.cmict.data.bpm.convert.task.BpmTaskConvert;
import com.cmict.data.bpm.dal.dataobject.definition.BpmProcessDefinitionExtDO;
import com.cmict.data.bpm.dal.dataobject.task.BpmTaskExtDO;
import com.cmict.data.bpm.service.definition.BpmProcessDefinitionService;
import com.cmict.data.bpm.service.task.BpmProcessInstanceService;
import com.cmict.data.bpm.service.task.BpmTaskService;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

/**
 * Flowable 流程实例 Api 实现类
 *
 * @author awesome
 * @author jason
 */
@RestController
@Validated
public class BpmTaskApiImpl implements BpmTaskApi {

    @Resource
    private BpmTaskService bpmTaskService;
    @Resource
    private BpmProcessDefinitionService definitionService;
    @Resource
    private BpmProcessInstanceService processInstanceService;

    @Override
    public CommonResult<BpmTaskExtDTO> getTaskExt(String remindId) {
        BpmTaskExtDO bpmtaskext = bpmTaskService.getBpmtaskext(Long.parseLong(remindId));
        BpmTaskExtDTO taskExtDTO = BpmTaskConvert.INSTANCE.convert3(bpmtaskext);
        return success(taskExtDTO);
    }

    @Override
    public CommonResult<PageResult<BpmTaskTodoPageRespDTO>> getTodoTaskPage(Long userId, BpmTaskTodoPageReqDTO pageVO) {
        BpmTaskTodoPageReqVO todoPageReqVO = BpmTaskConvert.INSTANCE.convert(pageVO);
        PageResult<BpmTaskTodoPageItemRespVO> taskPage = bpmTaskService.getTodoTaskPage(userId, todoPageReqVO);
        List<BpmTaskTodoPageRespDTO> taskTodoPageRespDTOS = taskPage.getList().stream().map(item -> BpmTaskConvert.INSTANCE.convertResult(item))
                .collect(Collectors.toList());
        PageResult<BpmTaskTodoPageRespDTO> pageResult = new PageResult<>(taskTodoPageRespDTOS, taskPage.getTotal());
        return CommonResult.success(pageResult);
    }

    @Override
    public CommonResult<List<BpmTaskTodoPageRespDTO>> getTodoTask(Long userId, Long formId) {
        List<BpmTaskTodoPageItemRespVO> todoTasks = bpmTaskService.getTodoTask(userId, formId);
        List<BpmTaskTodoPageRespDTO> taskItems = todoTasks.stream().map(item -> BpmTaskConvert.INSTANCE.convertResult(item)).collect(Collectors.toList());
        return CommonResult.success(taskItems);
    }

    @Override
    public CommonResult<Set<Integer>> getApproveRecord(Long formId) {
        List<BpmProcessDefinitionExtDO> processDefinitionExt = definitionService.getProcessDefinitionExt(formId);
        Set<String> processDefinitionIdSet = processDefinitionExt.stream().map(item -> item.getProcessDefinitionId()).collect(Collectors.toSet());
        Set<Integer> formRecordIdSet = processInstanceService.getProcessInstancesByDefinition(processDefinitionIdSet);
        return CommonResult.success(formRecordIdSet);
    }

    @Override
    public CommonResult<PageResult<BpmTaskDonePageItemRespDTO>> getDoneTaskPage(Long userId, BpmTaskDonePageReqDTO reqDTO) {
        BpmTaskDonePageReqVO pageReqVO = BpmTaskConvert.INSTANCE.convert(reqDTO);
        PageResult<BpmTaskDonePageItemRespVO> doneTaskPage = bpmTaskService.getDoneTaskPage(userId, pageReqVO);
        List<BpmTaskDonePageItemRespDTO> respDTOS = doneTaskPage.getList().stream().map(item -> BpmTaskConvert.INSTANCE.convertResult(item))
                .collect(Collectors.toList());
        PageResult<BpmTaskDonePageItemRespDTO> pageResult = new PageResult<>(respDTOS, doneTaskPage.getTotal());
        return CommonResult.success(pageResult);
    }

    @Override
    public CommonResult<List<BpmTaskDonePageItemRespDTO>> getDoneTask(Long userId, Long formId) {
        List<BpmTaskDonePageItemRespVO> doneTasks = bpmTaskService.getDoneTask(userId, formId);
        List<BpmTaskDonePageItemRespDTO> tasks = doneTasks.stream().map(item -> BpmTaskConvert.INSTANCE.convertResult(item)).collect(Collectors.toList());
        return CommonResult.success(tasks);
    }

}
